var app = getApp();
Page({
  data: {
    currentDate: "2017年05月03日",
    dayList: '',
    currentDayList: '',
    currentObj: '',
    currentDay: '',

    //日期初始化选中样式
    selectCSS: 'bk-color-day',
  },
  backto: function () {
    wx.navigateBack({})
  },
  onLoad: function (options) {
    var that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    app.ihttpGet("https://www.58zltc.com/api.php/Credit/sign_index/openid/" + app.globalData.openid, res => {
       that.setData({
         i:res.data
       })
    })


    var currentObj = this.getCurrentDayString()
    this.setData({
      currentDate: currentObj.getFullYear() + '-' + (currentObj.getMonth() + 1) + '-' + currentObj.getDate(),
      currentDay: currentObj.getDate(),
      today: currentObj.getDate(),
      currentObj: currentObj,
      /*  获取当前的年、月  */
      currentYear: currentObj.getFullYear(),
      currentMonth: (currentObj.getMonth() + 1),
      userInfo: app.globalData.userInfo
    })
    
    setTimeout(function(){
      that.setSchedule(currentObj);
    },300)
    
  },
  doDay: function (e) {
    var that = this;

    var currentObj = that.data.currentObj
    var Y = currentObj.getFullYear();
    var m = currentObj.getMonth() + 1;
    var d = currentObj.getDate();
    var str = ''
    if (e.currentTarget.dataset.key == 'left') {
      m -= 1
      if (m <= 0) {
        str = (Y - 1) + '-' + 12 + '-' + d
      } else {
        str = Y + '-' + m + '-' + d
      }
    } else {
      m += 1
      if (m <= 12) {
        str = Y + '-' + m + '-' + d
      } else {
        str = (Y + 1) + '-' + 1 + '-' + d
      }
    }
    currentObj = new Date(str)
    this.setData({
      currentDate: currentObj.getFullYear() + '-' + (currentObj.getMonth() + 1) + '-' + currentObj.getDate(),
      currentObj: currentObj,
      /*  获取当前的年、月  */
      currentYear: currentObj.getFullYear(),
      currentMonth: (currentObj.getMonth() + 1),
    })
    console.log("选择当前年：" + that.data.currentYear);
    console.log("选择当前月：" + that.data.currentMonth);
    this.setSchedule(currentObj);
  },
  getCurrentDayString: function () {
    var objDate = this.data.currentObj
    if (objDate != '') {
      return objDate
    } else {
      var c_obj = new Date()
      var a = c_obj.getFullYear() + '-' + (c_obj.getMonth() + 1) + '-' + c_obj.getDate()
      return new Date(a)
    }
  },
  setSchedule: function (currentObj) {
    var that = this
    var m = currentObj.getMonth() + 1
    var Y = currentObj.getFullYear()
    var d = currentObj.getDate();
    var dayString = Y + '-' + m + '-' + currentObj.getDate()
    var currentDayNum = new Date(Y, m, 0).getDate()
    var currentDayWeek = currentObj.getUTCDay() + 1
    var result = currentDayWeek - (d % 7 - 1);
    var firstKey = result <= 0 ? 7 + result : result;
    var currentDayList = [];
    var f = 0
    for (var i = 0; i < 42; i++) {
      let data = []
      if (i < firstKey - 1) {
        currentDayList[i] = ''
      } else {
        if (f < currentDayNum) {
          currentDayList[i] = f + 1;
          f = currentDayList[i]
        } else if (f >= currentDayNum) {
          currentDayList[i] = ''
        }
      }
    }
    that.setData({
      currentDayList: currentDayList
    })
    //选中数组
    var selectDayList = [];
    var data = that.data.i.sign;
    //处理选中数组
    for (var m = 0; m < currentDayList.length;m++){
      selectDayList[m]=0
    }
    for (var n = 0; n < data.length;n++){
      //当月
      if (parseInt(data[n].month) == that.data.currentMonth){
        var index = parseInt(data[n].day)
        selectDayList[index +1] = 1
      }
    }
    that.setData({
      selectDayList: selectDayList
    })
  },

  //选择具体日期方法--xzz1211
  selectDay: function (e) {
    var that = this;
    that.setData({
      currentDay: e.currentTarget.dataset.day,//选择的数据，非真实当前日期
      currentDa: e.currentTarget.dataset.day, //选择某月具体的一天
      currentDate: that.data.currentYear + '-' + that.data.currentMonth + '-' + e.currentTarget.dataset.day,//真实选择数据
    })
    console.log("当前选择日期：" + that.data.currentDate);

    //请求后台，判断一下。如果点击的天已经签到或者小于当前天数，无反应
    if (that.data.today == e.target.dataset.day){
      that.sign();
    }
  },
  sign:function(e){
    var that =this
    app.ihttpGet("https://www.58zltc.com/api.php/Credit/sign/openid/" + app.globalData.openid, res => {
      wx.showToast({
        icon: "none",
        title: res.data.msg,
      })
      setTimeout(function () {
        that.onLoad();
      }, 1500)
    })
  },
  //领取签到积分
  sign_gift:function(e){
    var day = e.currentTarget.dataset.day;
    var that = this
    app.ihttpGet("https://www.58zltc.com/api.php/Credit/sign_gift/openid/" + app.globalData.openid + "/day/" + day + "/credit/" + e.currentTarget.dataset.credit, res => {
      if(res.data.code >0){
        wx.showToast({
          icon: "none",
          title: res.data.msg,
        })
        setTimeout(function () {
          that.onLoad();
        }, 2000)
      }else{
        wx.showToast({
          icon: "success",
          title: res.data.msg,
        })
        setTimeout(function () {
          that.onLoad();
        }, 2000)
      }
    })
  }
})